using System;
using System.Collections.Generic;
using System.Text;

using System.ComponentModel;
using System.Drawing;

namespace Athena.Objects
{
    internal class BackgroundConverter : ExpandableObjectConverter
    {
        public override bool CanConvertFrom(
        ITypeDescriptorContext context, Type t)
        {

            if (t == typeof(string))
            {
                return true;
            }
            return base.CanConvertFrom(context, t);
        }

        public override object ConvertFrom(
        ITypeDescriptorContext context,
        System.Globalization.CultureInfo info,
        object value)
        {

            if (value is string)
            {
                try
                {
                    string s = (string)value;
                    // parse the format "xPos, yPos, width, height"
                    //

                    string[] array = s.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);

                    if (array.Length == 3)
                    {
                        Color oStartColor = Color.FromName((string)array[0]);
                        Color oEndColor = Color.FromName((string)array[1]);
                        ModelBackground.BackGroundType oType = (ModelBackground.BackGroundType)Enum.Parse(typeof(ModelBackground.BackGroundType), (string)array[2]); 
                        
                        return new ModelBackground(oStartColor, oEndColor, oType);
                    }
                }
                catch { }
                // if we got this far, complain that we
                // couldn't parse the string
                //
                throw new ArgumentException(
                "Can not convert '" + (string)value +
                "' to type ModelBackground");

            }
            return base.ConvertFrom(context, info, value);
        }

        public override object ConvertTo(ITypeDescriptorContext context,
        System.Globalization.CultureInfo culture,
        object value,
        Type destType)
        {
            if (destType == typeof(string) && value is ModelBackground)
            {
                ModelBackground temp = (ModelBackground)value;
                return String.Format("{0}; {1}; {2}", temp.StartColor, temp.EndColor, temp.Type);
            }
            return base.ConvertTo(context, culture, value, destType);
        }
    }
}
